<include file="Public:header" />

<div>
	<a href="javascript:" onclick="add();">添加</a> &nbsp;&nbsp;搜索：<input name="search" id="searchInput"  />
</div>
<script type="text/javascript" src="/Public/blocksit/blocksit.min.js"></script>
<link rel="stylesheet" type="text/css" href="/Public/blocksit/style.css" />

<script type="text/javascript">
var imgServer = '{:C("img_server")}';
var currentWidth = $(window).width();
$(document).ready(function(){	
	searchSite(1,'','');
	$(window).load(function() {	
		blocksit(currentWidth);
	}).resize(function() {
		blocksit(currentWidth);//瀑布流布局
	});
	
	//编辑
	$(this).on('click','.edit-btn',function(){
		var obj = $(this).parents('.grid');
		edit(obj)
	});
	
	//删除
	$(this).on('click','.del-btn',function(){
		var obj = $(this).parents('.grid');
		var id = obj.attr('data-id');
		$.jBox.confirm('确定删除吗','警告',function(v, h, f){
			if (v == 'ok'){
				$.jBox.tip('Loading...','loading');
				$.get('/admin.php?c=Ajax&a=site&do=delete&id='+id,function(data){
					var icon = data.status?'success':'error';
					if(data.status>0){
						$.jBox.tip(data.msg,icon);
						searchSite('','','');
					}else{
						jBox.tip(data.msg, icon);
					}
				})
			}
			return true;
		});
	});
	
	//快捷编辑
	$(this).on('change','.edit-input',function(){
		var obj = $(this).parents('.grid');
		var name = obj.find('[name=name]').val();
		var intro = obj.find('[name=intro]').val();
		var id = obj.attr('data-id');
		var url = '/admin.php?c=Ajax&a=site&do=save&id='+id;
		$.jBox.tip('数据提交中...','loading');
		$.post(url,{'name':name,'intro':intro},function(rs){
			var icon = rs.status?'success':'error';
			$.jBox.tip(rs.msg,icon);
		});
	});
	
	//下一页
	$(this).on('click','.next-page-btn',function(){
		$(this).html('请稍后...');
		var page = $(this).attr('data-next_page');
		var key = $(this).attr('data-key');
		searchSite(page,'append',key);
	});
	
	//检查url
	$(this).on('change','#edit-form input[name=url]',function(){
		var url = $(this).val();
		var id = $(this).attr('data-id');
		if(!/^[a-z]{3,20}$/.test(url)){
			$.jBox.tip('url必须为3~20个小写字母','error');
			return false;
		}
		$.jBox.tip('Loading...','loading');
		$.get('/admin.php?c=Ajax&a=site&do=checkUrl&url='+url+'&id='+id,function(data){
			if(data>0){
				$.jBox.tip('此url已经存在','error');
				$('#edit-form input[name=url]').val('');
			}else{
				$.jBox.tip('此url可以用','success');
			}
		})
	});
	
	//搜索
	$('#searchInput').change(function(){
		var key = $(this).val();
		if(!key){
			return ;
		}
		$('#blocksit-container').html('搜索中...');
		$('.next-page-btn').attr('data-key',key);
		searchSite(1,'',key);
	});
});

//编辑框
function edit(obj){
	var id = obj.attr('data-id');
	if(!id){
		$.jBox.tip('ID丢失,不能编辑','error');
		return false;
	}
	$.jBox.tip('Loading...','loading');
	$.get('/admin.php?c=Ajax&a=site&do=getInfo&id='+id,function(data){
		$.jBox.closeTip();
		var html = '<div id="edit-form"><input name="id" type="hidden" value="'+id+'" />'+$('#edit-bar').html()+'</div>';
		$.jBox(html, { title: "编辑", submit: saveData,width: 600 });
		
		var editForm = $('#edit-form');
		editForm.find('[name=name]').val(data.name);
		editForm.find('[name=url]').val(data.url).attr('data-id',id);
		editForm.find('[name=intro]').val(data.intro);
		editForm.find('[name=keyword]').val(data.keyword);
		editForm.find('[name=thumb]').val(data.thumb);
		editForm.find('[name=cid]').find('[value='+data.cid+']').attr("selected","selected");
		editForm.find('[name=sort]').find('[value='+data.sort+']').attr("selected","selected");
		editForm.find('.tupian').html('<img src="'+imgServer+data.thumb+'" style="max-width:150px; max-height:150px;" border=0 />');
		editForm.find('table').show();
		
		//上传图片
		editForm.find('.uploadBtn').on('change',function(){
			var url = editForm.find('[name=url]').val();
			if(!url){
				$.jBox.tip('上传封面失败,url不能空','error');
				return false;
			}
			editForm.find('#uploadSite').val('huahui/'+url);
			$.jBox.tip('Loading...','loading');
			editForm.find('.tupian').html('上传中...');
			editForm.find('.uploadImageForm').submit();
		});
	});
}	

//添加框
function add(){
	var html = '<div id="edit-form">'+$('#edit-bar').html()+'</div>';
	$.jBox(html, { title: "新增", submit: saveData,width:600 });
	var editForm = $('#edit-form');
	editForm.find('table').show();
	//上传图片
	editForm.find('.uploadBtn').on('change',function(){
		var url = editForm.find('[name=url]').val();
		if(!url){
			$.jBox.tip('上传封面失败,url不能空','error');
			return false;
		}
		editForm.find('#uploadSite').val('huahui/'+url);
		$.jBox.tip('Loading...','loading');
		editForm.find('.tupian').html('上传中...');
		editForm.find('.uploadImageForm').submit();
	});
	 
}

//上传成功回调
function uploadCallBack(domain,path){
	if(!path){
		$.jBox.tip(domain,'info');
		return false;
	}
	var editForm = $('#edit-form');
	editForm.find('[name=thumb]').val(path);
	editForm.find('.tupian').html('<img src="'+domain+path+'" style="max-width:150px; max-height:150px;" border=0 />');
	$.jBox.tip('上传成功','success');
}

//保存数据
var saveData = function (text, obj, form) {
	var id = form.id;
	if(form.name.length<2 || form.name.length>20 ){
		$.jBox.tip('名字必须为2~20字','error');
		return false;
	}
	if(!/^[a-z]{3,20}$/.test(form.url)){
		$.jBox.tip('url必须为3~20个小写字母','error');
		return false;
	}
	if(form.cid<=0){
		$.jBox.tip('请选择分类','error');
		return false;	
	}
	if(!form.thumb){
		$.jBox.tip('请上传封面','error');
		return false;
	}

	if(id){
		var obj = $('#blocksit-container .grid[data-id='+id+']');
		obj.find('[name=name]').val(form.name);
		obj.find('[name=intro]').val(form.intro);
		if(form.thumb){
			obj.find('.imgholder img').attr('src',imgServer+form.thumb+'');
		}
	}
	var url = '/admin.php?c=Ajax&a=site&do=save&id='+id;
	$.jBox.tip('数据提交中...','loading');
	$.post(url,form,function(rs){
		//添加时候重新加载
		if(!id){
			searchSite(1,'','');
		}
		if(rs.status){
			$.jBox.tip(rs.msg,'success');
		}else{
			$.jBox.tip(rs.msg,'error');
		}
	});
};

//获取列表
function searchSite(page,append,key){
	var url = '/admin.php?c=Ajax&a=site&do=search&page='+page+'&key='+key;
	$.get(url,function(data){
		var html = '';
		if(!data.list){
			$('#blocksit-container').html('暂无结果');
			return ;
		}
		$.each(data.list,function(i,val){
			html += '<div class="grid" data-id="'+val.id+'">'+
				'<div class="imgholder"><a href="javascript:" class="edit-btn"><img src="'+val['thumb150']+'" /></a></div>'+
				'<strong><input name="name" value="'+val.name+'" class="edit-input" /></strong>'+
				'<p><textarea name="intro" class="edit-input">'+val.intro+'</textarea></p>'+
				'<div class="meta">访问:'+val.viewnum+'&nbsp;推荐:'+val.sort+'&nbsp;<a href="/admin.php?m=Admin&c=Index&a=siteedit&id='+val.id+'">编辑</a> <a href="javascript:" class="del-btn">删除</a><br>'+val.updatetime+'</div>'+
			'</div>';
		});
		setTimeout(function(currentWidth){
			blocksit(currentWidth);
		},500);
		if(append == 'append'){
			$('#blocksit-container').append(html);
		}else{
			$('#blocksit-container').html(html);
		}
		
		//分页
		if(data.next_page){
			$('.next-page-btn').attr('data-next_page',data.next_page).show().html('下一页');
		}else{
			$('.next-page-btn').hide();
		}
	});
}
</script>
<div id="blocksit-container">
	加载中... 
</div>
<div class="next-page-btn hide">下一页</div>


<div id="edit-bar">
  <table width="98%" border="0" style="display:none;" cellpadding="5">
    <tr>
      <td width="15%" align="right" valign="top">名称</td>
      <td width="85%"><input name="name" type="text" class="edit-input1 name" /> *</td>
    </tr>
    <tr>
      <td align="right" valign="top">url</td>
      <td><input name="url" type="text" value="{$data['site']['url']}" class="edit-input1" /> *</td>
    </tr>
    <tr>
      <td align="right" valign="top">分类</td>
      <td>
      <select name="cid" id="cid">
        <option value="0" selected="selected">无分类</option>
        <volist name="data['SiteCategory']['list']" id="val">
        <option value="{$val['id']}">{$val['name']}</option>
        </volist>
      </select>
      推荐 
      <select name="sort" id="sort">
        <option value="0" selected="selected">--</option>
        <option value="1">1</option>
      </select>
            
      </td>
    </tr>
    <tr>
      <td align="right" valign="top">描述</td>
      <td><textarea name="intro" id="context" style="height:100px;" class="edit-input1">{$data['site']['intro']}</textarea></td>
    </tr>
    <tr>
      <td align="right" valign="top">关键词</td>
      <td><input name="keyword" type="text" value="{$data['site']['keyword']}" class="edit-input1" /></td>
    </tr>
    <tr>
      <td align="right" valign="top">封面</td>
      <td><form method="post" class="uploadImageForm" enctype="multipart/form-data" class="hide" target="iframe-form" action="/?c=Upload&a=upload">
<input type="file" name="upload" class="uploadBtn"  /><input type="hidden" name="thumb" /><input type="hidden" value="thumb" name="category"/> <input type="hidden" name="dir" id="uploadSite" />  *</form> <div class="tupian"></div></td>
    </tr>
    <tr>
      <td align="right" valign="top">&nbsp;</td>
      <td></td>
    </tr>
  </table>
</div>
<iframe id="iframe-form" name="iframe-form" src="" class="hide" ></iframe>
<include file="Public:footer" />
